package com.linghang.wusthelper.spider.library;

import com.linghang.wusthelper.dao.primary.LibAnnouncementDao;
import com.linghang.wusthelper.entity.primary.LibAnnouncement;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * @author origin
 * 图书馆公告爬虫
 */
public class LibAnnouncementSpider implements PageProcessor {

    private LibAnnouncementDao libAnnouncementDao;

    private Site site = Site.me().setTimeOut(3000).setRetryTimes(3)
            .setDomain("http://www.lib.wust.edu.cn")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
            .addHeader("Accept-Encoding", "gzip,deflate,sdch")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
            .addHeader("Connection", "keep-alive")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");

    public LibAnnouncementSpider(LibAnnouncementDao libAnnouncementDao) {
        this.libAnnouncementDao = libAnnouncementDao;
    }

    @Override
    public void process(Page page) {
        for (int i = 1; i <= 18; i++) {
            String title = page.getHtml().xpath("//*[@id=\"content\"]/tbody/tr/td[2]/div[4]/div[" + i + "]/div[1]/a/text()").get();
            String url = page.getHtml().xpath("//*[@id=\"content\"]/tbody/tr/td[2]/div[4]/div[" + i + "]/div[1]/a/@href").get();
            String time = page.getHtml().xpath("//*[@id=\"content\"]/tbody/tr/td[2]/div[4]/div[" + i + "]/div[2]/text()").get();
            if (libAnnouncementDao.existTitle(title) > 0) {
                break;
            }

            //截取地址后四位 如果是html就再爬取内容


            LibAnnouncement libAnnouncement = new LibAnnouncement();
            libAnnouncement.setAnnouncementTitle(title);
            libAnnouncement.setAnnouncementCreatetime(time.substring(2, time.length() - 1));
            StringBuilder content = new StringBuilder();
            Spider.create(new LibAnnContentSpider(content)).addUrl(url).thread(1).runAsync();
            synchronized (content) {
                try {
                    content.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            libAnnouncement.setContent(content.toString());
            libAnnouncement.setAnnouncementUrl(url);
            libAnnouncementDao.insert(libAnnouncement);
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
